package com.banciyuan.bcywebview.biz.main.group.groupselect;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2016/4/29.
 */
public class GroupSelectActivity extends BaseActivity {

    public static final int TYPE_HOT = 0;
    public static final int TYPE_WAIT = 1;

    private List<Fragment> mList = new ArrayList<>();
    private ViewPager mPager;
    private MyAdapter mAdapter;
    private TabLayout mTabLayout;
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;


    private GroupListFragment hotGroupListFramment;
    private AnswerListFragment hotanswerFragment;
    private GroupListFragment joinListFragment;
    private int select;
    private String[] tabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_discuss);
        getFragment();
        initArgs();
        initActionbar();
        initUi();
    }

    @Override
    protected void initArgs() {
        tabs=new String[]{getString(R.string.hot_topic),getString(R.string.topest_zan),getString(R.string.wait_for_you_join)};
        select = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, 0);
    }

    private void getFragment() {
        initHot();
        initWait();
        initAnswer();
        mList.add(hotGroupListFramment);
        mList.add(hotanswerFragment);
        mList.add(joinListFragment);
    }

    private void initHot() {
        Bundle bundle = new Bundle();
        hotGroupListFramment = new GroupListFragment();
        bundle.putInt("type", TYPE_HOT);
        hotGroupListFramment.setArguments(bundle);
    }

    private void initWait() {
        hotanswerFragment = new AnswerListFragment();
    }

    private void initAnswer() {
        Bundle bundle = new Bundle();
        joinListFragment = new GroupListFragment();
        bundle.putInt("type", TYPE_WAIT);
        joinListFragment.setArguments(bundle);
    }


    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.more_group));
    }

    @Override
    protected void initUi() {
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mTabLayout= (TabLayout) findViewById(R.id.tablayout);
        mPager.setOffscreenPageLimit(3);
        mPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mPager);
        mPager.setCurrentItem(select);
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

}
